package com.hzw.net.utils

import android.util.Base64
import java.security.InvalidAlgorithmParameterException
import java.security.InvalidKeyException
import java.security.NoSuchAlgorithmException
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

/**
 * AES加密
 */
object AESUtil {
    private const val CBC = "AES/CBC/NoPadding"
    // AES的偏移量
    private const val AES_IV = "qihuangshopstrin"

    /**
     * 加密
     * @param content
     * @param key
     * @return
     */
    fun encrypt(content: String, key: String): String {
        val ivSpec = IvParameterSpec(AES_IV.toByteArray())
        val keySpec = SecretKeySpec(key.toByteArray(), "AES")
        try {
            val cipher = Cipher.getInstance(CBC)
            val blockSize = cipher.blockSize

            val dataBytes = content.toByteArray()
            var plaintextLength = dataBytes.size
            if (plaintextLength % blockSize != 0) {
                plaintextLength += (blockSize - plaintextLength % blockSize)
            }
            val plaintext = ByteArray(plaintextLength)
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.size)

            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec)
            val encryptBytes = cipher.doFinal(plaintext)
            return Base64.encodeToString(encryptBytes, Base64.NO_WRAP)
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        } catch (e: NoSuchPaddingException) {
            e.printStackTrace()
        } catch (e: InvalidAlgorithmParameterException) {
            e.printStackTrace()
        } catch (e: InvalidKeyException) {
            e.printStackTrace()
        } catch (e: BadPaddingException) {
            e.printStackTrace()
        } catch (e: IllegalBlockSizeException) {
            e.printStackTrace()
        }
        return ""
    }

    /**
     * 解密
     * @param content
     * @param key
     * @return
     */
    fun decrypt(content: String?, key: String): String? {
        val ivSpec =
            IvParameterSpec(AES_IV.toByteArray())
        val keySpec =
            SecretKeySpec(key.toByteArray(), "AES")
        try {
            val cipher = Cipher.getInstance(CBC)
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec)
            var encryptBytes =
                cipher.doFinal(Base64.decode(content, Base64.NO_WRAP))
            if (encryptBytes.isNotEmpty()) {
                var trim = 0
                for (i in encryptBytes.indices.reversed()) if (encryptBytes[i].equals(0)) trim++
                if (trim > 0) {
                    val newArray = ByteArray(encryptBytes.size - trim)
                    System.arraycopy(
                        encryptBytes,
                        0,
                        newArray,
                        0,
                        encryptBytes.size - trim
                    )
                    encryptBytes = newArray
                }
            }
            return String(encryptBytes)
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        } catch (e: NoSuchPaddingException) {
            e.printStackTrace()
        } catch (e: InvalidAlgorithmParameterException) {
            e.printStackTrace()
        } catch (e: InvalidKeyException) {
            e.printStackTrace()
        } catch (e: BadPaddingException) {
            e.printStackTrace()
        } catch (e: IllegalBlockSizeException) {
            e.printStackTrace()
        }
        return ""
    }
}